/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package dao;

import domain.Note;
import domain.Userdom;
import java.util.HashSet;
import java.util.Set;
import javax.persistence.EntityManager;
import javax.persistence.EntityTransaction;

/**
 *
 * @author Ale
 */
public class NoteDao {

    EntityManager em;
    EntityTransaction tx;

    public NoteDao() {
        em = SingletonDao.getInstance().getEntityManager();
        tx = SingletonDao.getTx();
    }

    public Note getNote(Integer idNote) {
        Note n = (Note) em.createNativeQuery("select * from note as n where n.idNote='" + idNote + "'", Note.class).getSingleResult();
        return n;
    }

    public void saveNote(Note n) {
        tx.begin();
        em.persist(n);
        tx.commit();
    }

    public void removeNote(Note n) {
        tx.begin();
        em.remove(n);
        tx.commit();
    }

    public Set<Note> getNoteList(Userdom user) {
        Set<Note> s = new HashSet(em.createNativeQuery("select * from note as n where n.userFK='" + user.getNick() + "'", Note.class).getResultList());
        return s;
    }
}

